package sample;

import java.net.MalformedURLException;
import java.net.URL;
import java.text.ParseException;
import java.util.List;

import cn.Header;
import cn.eshore.bnet.sag.fax.ClientUtils;
import cn.eshore.bnet.sag.location.Active;
import cn.eshore.bnet.sag.location.ActiveResponse;
import cn.eshore.bnet.sag.location.GeographicInfo;
import cn.eshore.bnet.sag.location.GetGeographicInfo;
import cn.eshore.bnet.sag.location.GetGeographicInfoResponse;
import cn.eshore.bnet.sag.location.GetLocation;
import cn.eshore.bnet.sag.location.GetLocationResponse;
import cn.eshore.bnet.sag.location.GetMapURL;
import cn.eshore.bnet.sag.location.GetMapURLResponse;
import cn.eshore.bnet.sag.location.Location;
import cn.eshore.bnet.sag.location.LocationInfo;
import cn.eshore.bnet.sag.location.LocationService;
import cn.eshore.bnet.sag.location.LocationType;
import cn.eshore.bnet.sag.location.PolicyException_Exception;
import cn.eshore.bnet.sag.location.ServiceException_Exception;
import cn.eshore.bnet.sag.location.WebActionType;


/**
 * 定位Demo
 */
public class LocationDemo {
	private static Location location;
	
	public static void init(String wsdlurl) throws MalformedURLException {
		URL wsdlURL = new URL(wsdlurl);
		LocationService service = new LocationService(wsdlURL);
		location = service.getLocationServicePort();
	}
	
	public static void main(String[] args) {
		try {
			String wsdlURL = "http://125.88.125.218/sag-server-location/location?wsdl";//服务地址			

			String siCode = "92";//SI帐号
			String password = "abcd1234";//密码
			String accessNumber = "locationTest";//接入号  
			init(wsdlURL);	
			
			Header header = ClientUtils.createHeader(siCode,password, null, null, null, null,
					accessNumber, null);
			LocationDemo demo = new LocationDemo();
			String number = "18922296339";//18924138200
			
//测试粗定位、精定位。通过修改LocationType类型实现
			LocationInfo info = demo.getLocation(header,number);
			
//测试获取地理位置信息
			Float longitude = new Float(info.getLongitude());//经度
			Float latitude = new Float(info.getLatitude());//维度			
			demo.getGetGeographicInfo(header,longitude,latitude);
			
//测试自动激活
//			demo.autoActive(header,number);
			
//测试在地图上定位
//			List<String> numbers = new ArrayList<String>();
//			numbers.add("189********");
//			numbers.add("18924618011");			
//			demo.getMapURL(header,numbers);
			
//测试自动激活并定位(行业综合汇聚网关不提供，SI通过组合“active”和“getLocation”方法实现)
//			demo.autoActiveAndLocation(header,number);
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (PolicyException_Exception e) {
			System.out.println("ErrorCode : " + e.getErrorCode());
			System.out.println("ErrorMessage : " + e.getErrorMessage());
		} catch (ServiceException_Exception e) {
			System.out.println("ErrorCode : " + e.getErrorCode());
			System.out.println("ErrorMessage : " + e.getErrorMessage());
		}
	}
	
	public LocationInfo getLocation(Header header, String number)
			throws PolicyException_Exception, ServiceException_Exception {
		GetLocation getLocation = new GetLocation();
		getLocation.setAddress(number);//设置被定位手机号,只支持133、153、189开头的中国电信手机号码（C网）18924138200 ;13392522653
		getLocation.setRequestedAccuracy(1000);//要求精度
		getLocation.setAcceptableAccuracy(1100);//可接受的精度,未达到该要精度则返回定位失败错误
		getLocation.setLocationType(LocationType.CELL);//定位类型（GPS；GPS_ONE；CELL；GPS_CELL）参考《广东领航平台能力开放接口协议定位能力分册》	

		System.out.println("调用定位。。。。");
		GetLocationResponse response;

		response = location.getLocation(getLocation, header);
		LocationInfo info = response.getLocationInfo();
		System.out.println("手机" + info.getAddress() + "的定位信息如下:" + "经度:"
				+ info.getLongitude() + "纬度:" + info.getLatitude() + "海拔:"
				+ info.getAltitude() + "精度:" + info.getAccuracy());
		return info;
	}
	
	public void getGetGeographicInfo(Header header,Float longitude,Float latitude)
			throws PolicyException_Exception, ServiceException_Exception {
		GetGeographicInfo geographicInfo = new GetGeographicInfo();
		geographicInfo.setLongitude(longitude);//要求经度61745
		geographicInfo.setLatitude(latitude);//纬度00859

		System.out.println("调用地理位置信息查询。。。。");
		GetGeographicInfoResponse response;

		response = location.getGeographicInfo(geographicInfo, header);
		GeographicInfo info = response.getGeographicInfo();

		System.out.println("Province:" + info.getProvince());
		System.out.println("City:" + info.getCity());
		System.out.println("Dist:" + info.getDist());
		System.out.println("Area:" + info.getArea());
		System.out.println("Town:" + info.getTown());
		System.out.println("Village:" + info.getVillage());
		System.out.println("Poi:" + info.getPoi());
		System.out.println("Poitype:" + info.getPoiType());
		System.out.println("Direction:" + info.getDirection());
		System.out.println("Distance:" + info.getDistance());
		System.out.println("Roadname:" + info.getRoadName());
		System.out.println("RoadDirection:" + info.getRoadDirection());
		System.out.println("RoadDistance:" + info.getRoadDistance());
		System.out.println("RoadLevel:" + info.getRoadLevel());	

	}
	
	
	public boolean autoActive(Header header, String number)
			throws PolicyException_Exception, ServiceException_Exception {
		Active parameters = new Active();
		parameters.setAddress(number);
		ActiveResponse response;
		boolean isActive = false;
		response = location.active(parameters, header);
		isActive = response.isActiveResult();
		System.out.println("激活结果： " + isActive);
		return isActive;
	}
	
	
	public void getMapURL(Header header, List<String> numbers)
			throws PolicyException_Exception, ServiceException_Exception {
		GetMapURL parameters = new GetMapURL();
		for (String address : numbers) {
			parameters.getAddresses().add(address);
		}
		parameters.setLocationType(LocationType.CELL);// 定位类型（GPS；GPS_ONE；CELL；GPS_CELL）参考《广东领航平台能力开放接口协议定位能力分册》
		parameters.setWebActionType(WebActionType.MULTI_POINT_LOCATION);

		System.out.println("调用获取地图URL方法。。。。");
		GetMapURLResponse response;
		response = location.getMapURL(parameters, header);
		String mapURL = response.getGetMapURL();
		System.out.println("请在30秒内使用此地图URL获取地图：\n" + mapURL);
	}
	
	
	public void autoActiveAndLocation(Header header, String number)
			throws PolicyException_Exception, ServiceException_Exception {
		System.out.println("调用激活。。。。 ");
		boolean isActive = this.autoActive(header, number);
		if (isActive) {
			this.getLocation(header, number);
		} else {
			System.out.println("自动激活失败！！ ");
		}
	}
}
